package st.business.tepth.business.user.busnumber.workholder.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import io.reactivex.disposables.Disposable;
import st.business.tepth.R;
import st.business.tepth.base.OnItemChooseListener;
import st.business.tepth.business.bean.ProtocolBean;
import st.business.tepth.config.Url;
import st.shadow.library.core.net.callback.IError;
import st.shadow.library.core.net.callback.ISuccess;
import st.shadow.library.core.net.data.ObjConverter;
import st.shadow.library.core.net.rx.RxRestClient;
import st.shadow.library.ui.recycler.BaseDecoration;

/**
 * @author zhangyu
 * @date 2019/5/31
 */
public class DialogProtocol extends Dialog {
    private Disposable mDisposable;

    public DialogProtocol(@NonNull Context context,
                          String token, int repairId,
                          final OnItemChooseListener<Object> listener) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        final View view = LayoutInflater.from(context).
                inflate(R.layout.dialog_protocol, null);
        final RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        view.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        view.findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onChoose(null);
                }
                dismiss();
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        final BaseQuickAdapter<ProtocolBean, BaseViewHolder> adapter =
                new BaseQuickAdapter<ProtocolBean, BaseViewHolder>(R.layout.item_protocol,
                        null) {
                    @Override
                    protected void convert(BaseViewHolder viewHolder, ProtocolBean protocolBean) {
                        viewHolder.setText(R.id.tv_number, protocolBean.getSerial_no()
                        +".");
                        viewHolder.setText(R.id.tv_part, protocolBean.getPart());
                        viewHolder.setText(R.id.tv_item, protocolBean.getItem());
                        viewHolder.setText(R.id.tv_standard, protocolBean.getStandard());
                    }
                };
        recyclerView.addItemDecoration(BaseDecoration.
                create(Color.parseColor("#f0f0f0"), 1));
        recyclerView.setAdapter(adapter);
        adapter.setEmptyView(R.layout.view_loading, (ViewGroup) recyclerView.getParent());
        mDisposable = RxRestClient.builder()
                .url(Url.GetFinishCheckProtocol)
                .params("repairId", repairId)
                .params("token", token)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        adapter.setEmptyView(R.layout.view_search_empty,
                                (ViewGroup) recyclerView.getParent());
                        adapter.setNewData(convertToProtocols(response));
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        adapter.setEmptyView(R.layout.view_search_empty,
                                (ViewGroup) recyclerView.getParent());
                    }
                })
                .build()
                .post();
        setContentView(view);
    }

    private List<ProtocolBean> convertToProtocols(String response) {
        return new ObjConverter<List<ProtocolBean>>().convert(response
                , new TypeReference<List<ProtocolBean>>() {
                });
    }

    @Override
    public void dismiss() {
        if (mDisposable != null) {
            mDisposable.dispose();
        }
        super.dismiss();
    }

}
